#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

int triangleNumber(vector<int>& nums) {
    int n = nums.size();
    sort(nums.begin(), nums.end());
    int res = 0;
    for (int i = 0; i < n; i++)
        for (int j = i + 1; j < n; j++)
        {
            int l = j + 1, r = n-1;
            while (l < r)
            {
                int mid = l + r + 1 >> 1;
                if (nums[i] + nums[j] <= nums[mid])   r = mid - 1;
                else l = mid;
            }
            if (nums[i] + nums[j] <= nums[r])    r = j;
            res += r - j;
        }
    cout << res;
    return res;
}

int main()
{
    vector<int> v = { 2,2,3,4 };
    triangleNumber(v);
    return 0;
}